package com.leha.api.controller;

import com.github.pagehelper.PageInfo;
import com.leha.core.entity.mybatis.LhGjylQuestion;
import com.leha.core.entity.mybatis.LhRewardItem;
import com.leha.core.services.GameServices;
import com.leha.core.services.GameZZLDSServices;
import com.leha.core.utils.JFImgUtils;
import constants.APIErrorDict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import utils.APIResult;

import javax.ws.rs.QueryParam;

@RestController
@Api(value = "/admin", description = "微信小游戏后台管理")
@RequestMapping("/admin")
@ResponseStatus(HttpStatus.OK)
public class AdminController {
    @Autowired
    GameZZLDSServices zzldsServices;
    @Autowired
    GameServices gameServices;

    @RequestMapping(value = "/zzl/img", method = {RequestMethod.POST})
    @ApiOperation(value = "保存游戏图片", notes = "保存游戏图片")
    APIResult SaveImgs(Integer imgId, MultipartFile file){
        try{
            String url = JFImgUtils.uploadImgs(file);
            return new APIResult(zzldsServices.SaveImgs(imgId, url));
        } catch (Exception e){
            return new APIResult(APIErrorDict.AWSError.JFUPLOADErr);
        }
    }

    @RequestMapping(value = "/zzl/question", method = {RequestMethod.POST})
    @ApiOperation(value = "保存题目", notes = "保存游戏题目")
    APIResult SaveQuestions(LhGjylQuestion question){
        return new APIResult(zzldsServices.SaveQuestion(question));
    }

    @RequestMapping(value="/zzl/question/{questionId}", method = RequestMethod.GET)
    @ResponseBody
    public APIResult getQuestionsById(@PathVariable Integer questionId) {
        return new APIResult(zzldsServices.getQuestionById4Admin(questionId));
    }

    @RequestMapping(value="/zzl/questions", method = RequestMethod.GET)
    public APIResult getQuestions(Integer pgNum, Integer pgSize, Integer imgId, @RequestParam(required = false) String query) {
        return new APIResult(new PageInfo(zzldsServices.getQuestions4Admin(pgNum, pgSize, imgId, query)));
    }

    @RequestMapping(value="/zzl/imgs", method = RequestMethod.GET)
    public APIResult getImgs(Integer pgNum, Integer pgSize) {
        return new APIResult(new PageInfo(zzldsServices.getImages4Admin(pgNum, pgSize)));
    }

    @RequestMapping(value="/zzl/exchanges", method = RequestMethod.GET)
    public APIResult getUserExchage(Integer pgNum, Integer pgSize, Integer status, @RequestParam(required = false) String query) {
        return new APIResult(new PageInfo(zzldsServices.getExchanges4Admin(pgNum, pgSize, status)));
    }
    @RequestMapping(value = "/reward/save", method = {RequestMethod.POST})
    @ApiOperation(value = "修改发货状态", notes = "修改发货状态")
    APIResult SaveRewardStatus(Integer stats, Integer rewardId){
        return new APIResult(zzldsServices.SaveRewardStatus(rewardId, stats));
    }

    @RequestMapping(value = "/item", method = {RequestMethod.POST})
    @ApiOperation(value = "保存题目", notes = "保存游戏题目")
    APIResult SaveItems(LhRewardItem items, @RequestParam(required = false) MultipartFile file){
        if(file!=null) {
            try{
                String url = JFImgUtils.uploadImgs(file);
                items.setItemUrl(url);
            } catch (Exception e){
                return new APIResult(APIErrorDict.AWSError.JFUPLOADErr);
            }
        }
        return new APIResult(gameServices.SaveItems(items));
    }

    @RequestMapping(value="/items", method = RequestMethod.GET)
    public APIResult getItems(Integer pgNum, Integer pgSize) {
        return new APIResult(new PageInfo(gameServices.getItems(pgNum, pgSize)));
    }
    @RequestMapping(value="/item/{itemId}", method = RequestMethod.GET)
    public APIResult getItembyId(@PathVariable Integer itemId) {
        return new APIResult(gameServices.getItemById(itemId));
    }
}
